package org.jivesoftware.spark.crowd.ui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.jivesoftware.resource.Res;
import org.jivesoftware.resource.SparkRes;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smackx.muc.MultiUserChat;
import org.jivesoftware.smackx.muc.RoomInfo;
import org.jivesoftware.spark.SparkManager;
import org.jivesoftware.spark.component.TitlePanel;
import org.jivesoftware.spark.crowd.CrowdsManager;
import org.jivesoftware.spark.util.ModelUtil;
import org.jivesoftware.spark.util.ResourceUtils;
import org.jivesoftware.spark.util.log.Log;

public class CreationDialog extends JPanel{
	 private static final long serialVersionUID = -8391698290385575601L;
	    private JLabel nameLabel = new JLabel();	   
	    private JTextField nameField = new JTextField();
	    private JLabel titleLabel = new JLabel();	   
	    private JTextField titleField = new JTextField();
	    
	    private GridBagLayout gridBagLayout1 = new GridBagLayout();
	    
	    private UIEventManager uiEventManager;
	    public CreationDialog() {
	    	this.uiEventManager = CrowdsManager.getInstance().getUIEventManager();
	        try {
	            jbInit();
	        }
	        catch (Exception e) {
	            Log.error(e);
	        }
	    }

	    private void jbInit() throws Exception {
	        this.setLayout(gridBagLayout1);
	        
	        nameLabel.setText("输入创建群的名称");
	        nameLabel.setFont(new Font("微软雅黑",Font.PLAIN,12));
	        titleLabel.setText("输入创建群的主题");
	        titleLabel.setFont(new Font("微软雅黑",Font.PLAIN,12));
	        
	        this.add(nameLabel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
	        
	        this.add(nameField, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
	                 
	        this.add(titleLabel, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
	        
	        this.add(titleField,new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
//	        ResourceUtils.resLabel(nameLabel, nameField, "输入创建群的名称");
//	        ResourceUtils.resLabel(titleLabel,titleField, "输入创建群的主题");
	        
	    }

	    public void showDialog(Component parent) {
	        final JOptionPane pane;
	        final JDialog dlg;

	        TitlePanel titlePanel;

	        // Create the title panel for this dialog
	        titlePanel = new TitlePanel("创建群", "", SparkRes.getImageIcon(SparkRes.BLANK_24x24), true);

	        // Construct main panel w/ layout.
	        final JPanel mainPanel = new JPanel();
	        mainPanel.setLayout(new BorderLayout());
	        mainPanel.add(titlePanel, BorderLayout.NORTH);

	        // The user should only be able to close this dialog.
	        Object[] options = {Res.getString("create"), Res.getString("close")};
	        pane = new JOptionPane(this, JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null, options, options[0]);

	        mainPanel.add(pane, BorderLayout.CENTER);

	        JOptionPane p = new JOptionPane();
	        dlg = p.createDialog(parent, "创建群");
	        dlg.pack();
	        dlg.setSize(400, 200);
	        dlg.setContentPane(mainPanel);
	        dlg.setLocationRelativeTo(parent);


	        PropertyChangeListener changeListener = new PropertyChangeListener() {
	            public void propertyChange(PropertyChangeEvent e) {
	                Object o = pane.getValue();
	                if (o instanceof Integer) {
	                    dlg.setVisible(false);
	                    return;
	                }

	                String value = (String)pane.getValue();
	                if (Res.getString("close").equals(value)) {
	                    dlg.setVisible(false);
	                }
	                else if (Res.getString("create").equals(value)) {
	                    boolean isValid = validatePanel();
	                    if (isValid) {
	                        final String crowdName = nameField.getText().replaceAll(" ", "_");
	                        final String title = titleField.getText();
	                        new Thread(new Runnable(){
								@Override
								public void run() {
									uiEventManager.creatNewCrowd(crowdName, title);								
								}
	                        	
	                        }).start();
	                        
	                        dlg.setVisible(false);
	                        }
	                    
	                    else {
	                        pane.setValue(JOptionPane.UNINITIALIZED_VALUE);
	                    }
	                }
	            }
	        };

	        pane.addPropertyChangeListener(changeListener);
	        nameField.requestFocusInWindow();

	        dlg.setVisible(true);
	        dlg.toFront();
	        dlg.requestFocus();

	        
	    }

	    private boolean validatePanel() {
	        String roomName = nameField.getText();
	        if (!ModelUtil.hasLength(roomName)) {
	            showError("名称不能为空");
	            nameField.requestFocus();
	            return false;
	        }
	        return true;
	    }


	    private void showError(String errorMessage) {
	        JOptionPane.showMessageDialog(this, errorMessage, Res.getString("title.error"), JOptionPane.ERROR_MESSAGE);
	    }

	    public boolean isPermanent() {
	        return true;
	    }
	    /**
	     * Returns the Room name of the RoomCreationDialog
	     *
	     * @return The Room name
	     */
	    public String getRoomName() {
	        return nameField.getText();
	    }
	    /**
	     * Returns the Topic of the RoomCreationDialog
	     * @return The Rooms Topic
	     */
	 
}
